/**
 * SD模型模板类型定义
 * 基于 public/template.json 的数据结构
 */

// 基础模板接口
export interface BaseTemplate {
  id: string;
  name: string;
  description: string;
  image: string;
  base_model: string;
  lora_model?: string;
  lora_output_name?: string;
  lora_weight?: number;
  vae?: string;
  prompt: string;
  negative_prompt: string;
  sampler_name: string;
  scheduler?: string;
  steps: number;
  cfg_scale: number;
  width: number;
  height: number;
  seed: number;
  clip_skip?: number;
  created_at?: string;
  updated_at?: string;
}

// Txt2Img 模板接口
export interface Txt2ImgTemplate extends BaseTemplate {
  type: 'txt2img';
}

// Img2Img 模板接口
export interface Img2ImgTemplate extends BaseTemplate {
  type: 'img2img';
  denoising_strength: number;
}

// Postprocess 模板接口
export interface PostprocessTemplate {
  id: string;
  name: string;
  description: string;
  image: string;
  type: 'postprocess';
  resize_mode: number;
  upscaling_resize: number;
  upscaling_resize_w: number;
  upscaling_resize_h: number;
  upscaling_crop: boolean;
  upscaler_1: string;
  upscaler_2: string;
  extras_upscaler_2_visibility: number;
  created_at?: string;
  updated_at?: string;
}

// 联合类型
export type SDTemplate = Txt2ImgTemplate | Img2ImgTemplate | PostprocessTemplate;

// 模板类型枚举
export enum TemplateType {
  TXT2IMG = 'txt2img',
  IMG2IMG = 'img2img',
  POSTPROCESS = 'postprocess'
}

// API 请求/响应类型
export interface CreateTemplateRequest {
  template: Omit<SDTemplate, 'id' | 'created_at' | 'updated_at'>;
}

export interface UpdateTemplateRequest {
  template: Partial<Omit<SDTemplate, 'id' | 'created_at' | 'updated_at'>>;
}

export interface TemplateListResponse {
  success: boolean;
  data?: SDTemplate[];
  total?: number;
  page?: number;
  pageSize?: number;
  error?: string;
}

export interface TemplateResponse {
  success: boolean;
  data?: SDTemplate;
  error?: string;
}

export interface DeleteTemplateResponse {
  success: boolean;
  message?: string;
  error?: string;
}

// 查询参数
export interface TemplateQueryParams {
  type?: TemplateType;
  page?: number;
  pageSize?: number;
  search?: string;
  sortBy?: 'name' | 'created_at' | 'updated_at';
  sortOrder?: 'asc' | 'desc';
}

// 数据库表结构（用于初始化）
export interface TemplateTableSchema {
  id: string;           // 主键
  type: TemplateType;   // 模板类型
  name: string;         // 模板名称
  description: string;  // 描述
  image: string;        // 预览图片路径
  data: string;         // JSON格式的完整模板数据
  created_at: string;   // 创建时间
  updated_at: string;   // 更新时间
}

// 验证错误类型
export interface ValidationError {
  field: string;
  message: string;
}

export interface ValidationResult {
  isValid: boolean;
  errors: ValidationError[];
}